package com.chengyi.serialport.demo.controller

import android.content.Context
import android_serialport_api.SerialPortFinder
import com.chengyi.serialport.demo.MyApplication
import com.chengyi.serialport.demo.R
import com.chengyi.serialport.demo.entity.ActivityEntity
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.jessyan.autosize.AutoSizeCompat


object AppController {
    val activityList: ArrayList<ActivityEntity> by lazy {
        val context = MyApplication.application
        arrayListOf(
            ActivityEntity.create("初始化密钥", "secretInit"),
            ActivityEntity.create(context.getString(R.string.activity_ry_pcb_weight5_title), "activity_ry_pcb_weight5_title"),
            ActivityEntity.create(context.getString(R.string.activity_ry_pcb_lock_title), "ActivityRyPcbLockDemo"),
            ActivityEntity.create(context.getString(R.string.activity_face_test_title), "ActivityFaceTest"),
            ActivityEntity.create(context.getString(R.string.activity_jie_pu_wei_printer_title), "ActivityJiePuWeiPrinter"),
        )
    }

    private fun showMenuDialog(context: Context, data: Array<String>, callback: BindingCommand<String>) {
        AutoSizeCompat.cancelAdapt(context.resources)
        QMUIDialog.MenuDialogBuilder(context).addItems(data) { dialog, which ->
            dialog.dismiss()
            callback.execute(data[which])
        }.setCanceledOnTouchOutside(true).setCancelable(true).show()
        AutoSizeCompat.autoConvertDensityOfGlobal(context.resources)
    }
    fun getOnSerialPortPathClick(context: Context, callback: BindingCommand<String>) = BindingAction {
        showMenuDialog(context, SerialPortFinder().allDevicesPath, callback)
    }

    fun getOnBaudrateClick(context: Context, callback: BindingCommand<String>) = BindingAction {
        val data = arrayOf(0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000)
            .map { "$it" }.toTypedArray()
        showMenuDialog(context, data, callback)
    }
}